home *** CD-ROM | disk | FTP | other *** search
/ ADA Programming Guide / ADA Programming Guide.iso / ada_gwu / errs.c < prev    next >
C/C++ Source or Header  |  1996-01-30  |  5KB  |  187 lines

  1. /*
  2.  * Copyright (C) 1985-1992  New York University
  3.  * 
  4.  * This file is part of the Ada/Ed-C system.  See the Ada/Ed README file for
  5.  * warranty (none) and distribution info and also the GNU General Public
  6.  * License for more details.
  7.  
  8.  */
  9. #include "hdr.h"
  10. #include "ada.h"
  11. #include "vars.h"
  12. #include "ifile.h"
  13. #include "libfp.h"
  14. #include "pspansp.h"
  15. #include "adalexp.h"
  16. #include "errsp.h"
  17.  
  18. static void printerr(Span, Span, char *);
  19. static void err_display(char *);
  20. static void write_error(int, char *, Span, Span);
  21.  
  22. /* This file contains functions for printing error messages.
  23.    To print an error message given the complete message and spans,
  24.    printerr is used. To print a syntax error, syntax_err is
  25.    used.
  26.  */
  27.  
  28.  
  29. /* Printerr: print error message to the terminal 
  30.  * this should be called only in case some debugging option is set (-ert)
  31.  * Error message is printed to stdout if termopt set, to errfile if redopt or
  32.  * erropt set 
  33.  * If the error is not on the most recently printed source line, print the line
  34.  * (or last line if it spans multiple lines) first, followed by an underlining
  35.  * or pointer to error location.
  36.  */
  37.  
  38.  
  39. static void printerr(Span lspan, Span rspan, char *msg)
  40.                                                             /*;printerr*/
  41. {
  42.     int i;
  43.     char tmp_str[150];
  44.  
  45.     if (lspan->line==rspan->line || lineno - rspan->line < NUM_LINES)
  46.     {
  47.         /* print source line (or last line ) */
  48.         if (rspan->line != lineno   &&  /* error at current line */
  49.         !(feof(adafile) && lspan->line==lineno-1 &&colno==1) ) {
  50.             /* error is at last line in source */
  51.             sprintf(tmp_str, "%5d:  %s\n", rspan->line, source_buf[
  52.               (src_index + NUM_LINES - (lineno - rspan->line)) % NUM_LINES]);
  53.             err_display(tmp_str);
  54.         }
  55.         /* underline */
  56.         if (lspan->line != rspan->line) {
  57.             err_display(". . . . ");
  58.             for (i = rspan->col; --i;)
  59.                 err_display("-");
  60.             err_display(">\n");
  61.         }
  62.         else { /* error spans more than one line */
  63.             for (i = lspan->col + 8; --i;)
  64.                 err_display(" ");
  65.             if (rspan->col - lspan->col <= 1)
  66.                 err_display("^");
  67.             else {
  68.                 err_display("<");
  69.                 for (i = rspan->col - lspan->col - 1; i--;)
  70.                     err_display("-");
  71.                 err_display(">");
  72.             }
  73.             err_display("\n");
  74.         }
  75.     }
  76.     else {
  77.         sprintf(tmp_str, "-- Between line %d column %d and line %d column %d\n",
  78.           lspan->line, lspan->col, rspan->line, rspan->col);
  79.         err_display(tmp_str);
  80.     }
  81.     err_display(msg);
  82.     err_display("\n\n");
  83. }
  84.  
  85. static void err_display(char *str)                            /*;err_display*/
  86. {
  87.     if (termopt)
  88.         printf("%s", str);
  89. #ifdef DEBUG
  90.     if (trcopt)
  91.         fprintf(errfile, "%s", str);
  92. #endif
  93. }
  94.  
  95.  
  96. /* Syntax_err: Report an error detected during parsing actions */
  97.  
  98. void syntax_err(Span lspan, Span rspan, char *msg)
  99.                                                             /*;syntax_err*/
  100. {
  101.     char newmsg[300];
  102.  
  103.     errors++;
  104.     write_error(ERR_SYNTAX, msg, lspan, rspan);
  105.     sprintf(newmsg, "*** Syntax error: %s", msg);
  106.     if (debugopt)
  107.         printerr(lspan, rspan, newmsg);
  108. }
  109.  
  110.  
  111. void match_error(int id1, int id2, char *construct, Span lspan,
  112.   Span rspan)                                    /*;match_error*/
  113. {
  114.     /* Match_error: Report an error in matching two identifiers */
  115.     char msg[200];
  116.  
  117.     sprintf(msg, "%s at beginning of %s does not match %s",
  118.       namelist(id1), construct, namelist(id2));
  119.     syntax_err(lspan, rspan, msg);
  120. }
  121.  
  122. void prs_warning(Span lspan, Span rspan,  char *msg)
  123.                                                             /*;prs_warning*/
  124. {
  125.     /* Prs_warning: Report a warning message */
  126.     char newmsg[200];
  127.  
  128.     write_error(ERR_WARNING, msg, lspan, rspan);
  129.     sprintf(newmsg, "*** Warning: %s", msg);
  130.     if (debugopt)
  131.         printerr(lspan, rspan, newmsg);
  132. }
  133.  
  134.  
  135. void lexerr(int line, int col1, int col2, char *msg)            /*;lexerr*/
  136. {
  137.     /* Lexerr: Report an error detected by the lexical scanner */
  138.     char nmsg[300];
  139.     Span_s span1, span2;
  140.  
  141.     errors++;
  142.     span1.line = span2.line = line;
  143.     span1.col = col1;
  144.     span2.col = col2;
  145.     write_error(ERR_LEXICAL, msg, &span1, &span2);
  146.     sprintf(nmsg, "*** Lexical Error: %s", msg);
  147.     if (debugopt)
  148.         printerr(&span1, &span2, nmsg);
  149. }
  150.  
  151. /* This file contains functions dealing with files that need to be
  152.    written so other programs can read them.
  153. */
  154.  
  155.  
  156. /* So that the error messages can be merged with the listing of the
  157.    source file, and so that the pragmas LIST and PAGE are taken into
  158.    consideration properly, msgfile is written with information about
  159.    occurrances of the above. The format is as follows
  160.  
  161.    error_or_pragma_type lspan rspan
  162.    error_mssage
  163.  
  164.    where lspan and rspan are each two integers, error_or_pragma_type
  165.    is the type of error or pragma (defined in msgs.h), and error_message
  166.    is the error message in the case of an error. There is no error
  167.    message for pragmas (though there may be a seperate set of lines
  168.    for an error message which is there because of the pragma
  169. */
  170.  
  171.  
  172. void write_pragma(int pragma_type, Span lspan, Span rspan)
  173.                                                             /*;write_pragma*/
  174. {
  175.     /* Write_pragma: Writes data about pragma LIST and PAGE to the msgfile */
  176.     fprintf(msgfile, "%d %d %d %d %d\n", pragma_type, lspan->line,
  177.       lspan->col, rspan->line, rspan->col);
  178. }
  179.  
  180. static void write_error(int error_type, char *msg, Span lspan,
  181.   Span rspan)                                    /*;write_error*/
  182. {
  183.     /* Write_error: Write data about errors to msgfile */
  184.     fprintf(msgfile, "%d %d %d %d %d\t%s\n", error_type, lspan->line,
  185.       lspan->col, rspan->line, rspan->col, msg);
  186. }
  187.